﻿import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class JsonUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//读取json文件
		StringBuffer buff = readJsonFile("F:/test.txt");
		// 将字符串转换成jsonObject对象
		JSONObject jo = JSONObject.fromObject(buff.toString());
		//jsonObject对象换成HashMap
		HashMap map = new HashMap();
		JsonObjectHashMap(jo, map);
		System.out.println(JSONObject.fromObject(map));
		//获取对象
		HashMap obj=GetHashMap(map,"obj.properties");
		System.out.println(JSONObject.fromObject(obj));
		System.out.println(obj.get("thumbSrc"));
		//获取值
		String str=GetHashMapValue(map,"obj.properties.thumbSrc").toString();
		System.out.println(str);
		//获取集合
		List<HashMap> objArr=GetListHashMap(map,"obj.elements");
		System.out.println(JSONArray.fromObject(objArr));
		//循环集合对象
		for (HashMap m : objArr) {
			List<HashMap> a=GetListHashMap(m,"properties.anim");
			System.out.println(JSONArray.fromObject(a));
		}
	}
	
	/**
	 * 根据属性索引,获取对象
	 */
	public static Object GetHashMapValue(HashMap map,String indexPath) {
		Object returnObj=null;
		try {
			HashMap mapTemp = new HashMap();
			List<HashMap> ListMap = new ArrayList<HashMap>(); 
			String[] keyArray = indexPath.split("\\.");
			for (int i = 0; i < keyArray.length; i++) {
				if(i==0){
					if (map.get(keyArray[i]) instanceof HashMap) {
						mapTemp=(HashMap)map.get(keyArray[i]);
						returnObj=mapTemp;
					}else if (map.get(keyArray[i]) instanceof List) {
						ListMap=(List<HashMap>)map.get(keyArray[i]);
						returnObj=ListMap;
					}else{
						returnObj=map.get(keyArray[i]);
						break;
					}
				}else if (mapTemp.get(keyArray[i]) instanceof HashMap) {
					mapTemp=(HashMap)mapTemp.get(keyArray[i]);
					returnObj=mapTemp;
				}else if (mapTemp.get(keyArray[i]) instanceof List) {
					ListMap=(List<HashMap>)mapTemp.get(keyArray[i]);
					returnObj=ListMap;
				}else{
					returnObj=mapTemp.get(keyArray[i]);
					break;
				}
			}	
			
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return returnObj;
	}
	
	/**
	 * 根据属性索引,获取HashMap
	 * @param map
	 * @param indexPath
	 * @return
	 */
	public static HashMap GetHashMap(HashMap map,String indexPath) {
		Object temp=GetHashMapValue(map,indexPath);
		return temp==null?null:(HashMap)temp;
	}
	
	/**
	 * 根据属性索引,获取List<HashMap>
	 * @param map
	 * @param indexPath
	 * @return
	 */
	public static List<HashMap> GetListHashMap(HashMap map,String indexPath) {
		Object temp=GetHashMapValue(map,indexPath);
		return temp==null?null:(List<HashMap>)temp;
	}

	/**
	 * JSONObject转HashMap
	 * @param jo
	 * @param map
	 * @param isAllAttr 是否获取子对象属性
	 */
	public static void JsonObjectHashMap(JSONObject jo, HashMap map ,boolean isAllAttr) {
		
		for (Iterator keys = jo.keys(); keys.hasNext();) {
			try {
				String key = keys.next().toString();
				
				if (jo.get(key) instanceof JSONObject) {
					JSONObject joTemp=(JSONObject)jo.get(key);
					if (!joTemp.isNullObject()) {
						HashMap mapChaild = new HashMap();
						map.put(key, mapChaild);
						JsonObjectHashMap(joTemp, mapChaild,isAllAttr);
					} else {
						map.put(key, null);
					}
					continue;

				} else if (jo.get(key) instanceof JSONArray) {
					JSONArray joTemp=(JSONArray)jo.get(key);
					List<HashMap> ListMap = new ArrayList<HashMap>();
					if (!joTemp.isEmpty()) {
						map.put(key, ListMap);
						JsonArrayHashMap(joTemp, ListMap,isAllAttr);
					} else {
						map.put(key, ListMap);
					}
					continue;
				} else {
					map.put(key, jo.get(key));
				}
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * JSONObject转HashMap
	 * @param jo
	 * @param map
	 */
	public static void JsonObjectHashMap(JSONObject jo, HashMap map ) {
		JsonObjectHashMap(jo,map,true);
	}
	
	/**
	 * JSONArray转List<HashMap>
	 * @param joArr
	 * @param ListMap
	 * @param isAllAttr 是否获取子对象属性
	 */

	public static void JsonArrayHashMap(JSONArray joArr, List<HashMap> ListMap ,boolean isAllAttr) {
		
		for (int i = 0; i < joArr.size(); i++) {
			try {
				HashMap map = new HashMap();
				ListMap.add(map);
				if (joArr.get(i) instanceof JSONObject) {

					JsonObjectHashMap((JSONObject) joArr.get(i), map,isAllAttr);
					continue;
				}
				if (joArr.get(i) instanceof JSONArray) {

					JsonArrayHashMap((JSONArray) joArr.get(i), ListMap,isAllAttr);
					continue;
				}

			} catch (JSONException e) {
				e.printStackTrace();
			}

		}

	}

	/**
	 * JSONArray转List<HashMap>
	 * @param joArr
	 * @param ListMap
	 */
	public static void JsonArrayHashMap(JSONArray joArr, List<HashMap> ListMap ) {
		JsonArrayHashMap(joArr,ListMap,true);
	}
	
	/**
	 * 读取JSON文件
	 * @param filePath
	 * @return
	 */
	public static StringBuffer readJsonFile(String filePath,String encoding) {
		StringBuffer sb = new StringBuffer();
		try {
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					// System.out.println(lineTxt);
					sb.append(lineTxt);
				}
				read.close();
			} else {
				System.out.println("--------------找不到指定的文件--------------");
			}
		} catch (Exception e) {
			System.out.println("-------------------读取文件内容出错------------------");
			e.printStackTrace();
		}

		return sb;
	}

	
	
	/**
	 * 读取JSON文件
	 * @param filePath
	 * @return
	 */
	public static StringBuffer readJsonFile(String filePath) {
		return readJsonFile(filePath,"GBK");
	}
}
